package cn.itsource.system.controller;

import cn.itsource.system.domain.Log;
import cn.itsource.system.query.LogQuery;
import cn.itsource.system.service.ILogService;

import cn.itsource.util.ExcelUtil;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private ILogService logService;
    @GetMapping
    public List<Log> findAll(){
        return logService.findAll();
    }
    @GetMapping("/{id}")
    public Log findOne(@PathVariable("id")Long id){
        return logService.findOne(id);
    }

    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Log log){
        try {
            if(log.getId()==null){
                logService.add(log);
            }else {
                logService.update(log);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            logService.del(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @PatchMapping
    public JsonResult delete(@RequestBody Long[] ids){
        try {
            logService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @PostMapping
    public PageInfo<Log> pageQuery(@RequestBody LogQuery logQuery){
        PageInfo<Log> logPageInfo = logService.queryPage(logQuery);
        return logPageInfo;
    }
    /**
     * 导出
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Log> log = logService.findAll();
        ExcelUtil.exportExcel(log,"操作日志表","操作日志",Log.class,"Log",response);
    }

}
